<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:index.php');
}

$accountId     = isset($_GET['accountId']) ? $_GET['accountId'] : 0;
$accountName   = "";
$partyCode     = "";
$openingDrCr   = "";
$opening       = 0;
$totalCrAmount = 0;
$totalDrAmount = 0;
$finalAmount   = 0;
$summDetail    = isset($_GET['summDetail']) ? $_GET['summDetail'] : 0;
$accArray      = array();
$a = 0;
if($accountId > 0)
{
	$selectAccount = "SELECT accountName,openingDrCr,opening,isParty,partyCode
	                    FROM account
	                   WHERE accountId = ".$accountId;
	$selectAccountRes = mysql_query($selectAccount);
	if($accRow = mysql_fetch_array($selectAccountRes))
	{
		if($accRow['isParty'] == 1)
		{
      $partyCode = $accRow['partyCode'];
		}
	  $accountName = $accRow['accountName'];
	  $openingDrCr = $accRow['openingDrCr'];
	  $opening     = $accRow['opening'];
	  $totDrAmount = 0;
	  $totCrAmount = 0;
		$selTransDetail = "SELECT transmaster.transMasterId,transdetail.crAmount,transdetail.drAmount,transmaster.transDate,
		                          transmaster.voucherNo,transmaster.loanId
		                     FROM transmaster
		                     LEFT JOIN transdetail ON transdetail.transMasterId = transmaster.transMasterId
		                    WHERE (crAccountId = ".$accountId."
		                       OR drAccountId = ".$accountId.")";
		$selTransDetailRes = mysql_query($selTransDetail);
		while($detRow = mysql_fetch_array($selTransDetailRes))
		{
		  $accArray[$a]['transMasterId'] = $detRow['transMasterId'];
		  $accArray[$a]['transDate']     = $detRow['transDate'];
		  $accArray[$a]['voucherNo']     = $detRow['voucherNo'];
		  $accArray[$a]['drAmount']      = $detRow['drAmount'];
		  $accArray[$a]['crAmount']      = $detRow['crAmount'];
		  $accArray[$a]['loanId']        = $detRow['loanId'];
		  $totDrAmount += $detRow['drAmount'];
		  $totCrAmount += $detRow['crAmount'];
		  $a++;
		}
		if($openingDrCr == 'Cr')
		{
		  $totalCrAmount = $totCrAmount + $opening;
		}
		else
		{
		  $totalCrAmount = $totCrAmount;
		}
		if($openingDrCr == 'Dr')
		{
		  $totalDrAmount = $totDrAmount + $opening;
		}
		else
		{
		  $totalDrAmount = $totDrAmount;
		}
		$finalAmount = $totalCrAmount - $totalDrAmount;
	}
}
$partyArray = array();
$l=0;
$selectParty = "SELECT accountId,accountName
                  FROM account";
$selectPartyRes = mysql_query($selectParty);
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
	$partyArray['accountId'][$l]    = $partyRow['accountId'];
	$partyArray['accountName'][$l]  = $partyRow['accountName'];
	$l++;
}
//select Account : End
include("bottom.php");
$smarty->assign('accArray',$accArray);
$smarty->assign('accountName',$accountName);
$smarty->assign('partyCode',$partyCode);
$smarty->assign('openingDrCr',$openingDrCr);
$smarty->assign('opening',$opening);
$smarty->assign('totalDrAmount',$totalDrAmount);
$smarty->assign('totalCrAmount',$totalCrAmount);
$smarty->assign('finalAmount',$finalAmount);
$smarty->assign('partyArray',$partyArray);
$smarty->assign('summDetail',$summDetail);
$smarty->assign('accountId',$accountId);
$smarty->display('summaryDetail.tpl');  
?>